﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using MonitorServer;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace MonitorClient
{

    class Program
    {
        private static bool isclosing = false;
        static MonitorTask task;

        static long Seconds = 15;

        static void Main(string[] args)
        {
            ReadArgements(args);
            Init();

            SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCtrlCheck), true);

            Console.WriteLine("Client Started");
            //Console.WriteLine(string.Format("Exit after {0} Second(s).", Seconds));
            //Console.WriteLine("Please input 'stop' to close");

            Stopwatch watch = new Stopwatch();
            watch.Start();
            while (!isclosing)
            {
                //if (watch.ElapsedMilliseconds > Seconds * 1000)
                //{
                //    task.Stop();
                //    break;
                //}
                ////string key = Console.ReadLine();
                ////if (key == "stop")
                ////{
                ////    task.Stop();
                ////    break;
                ////}
            }
        }
        static void Init()
        {
            ChannelFactory<IMessageQueue> channelFactory = new ChannelFactory<IMessageQueue>("MessageQueueService");
            IMessageQueue service = channelFactory.CreateChannel();
            if (service == null)
            {
                Console.WriteLine("Service can't be created.");
                return;
            }
            try
            {
                task = new MonitorTask(service);
                task.Start();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        static void ReadArgements(string[] args)
        {
            for (int i = 0; i < args.Length; i++)
            {
                if (args[i].ToLower().Trim() == "-t")
                {
                    if (i < args.Length - 1)
                    {
                        long.TryParse(args[i + 1], out Seconds);
                    }
                }
            }

        }
        private static bool ConsoleCtrlCheck(CtrlTypes ctrlType)
        {
            switch (ctrlType)
            {
                case CtrlTypes.CTRL_C_EVENT:

                case CtrlTypes.CTRL_BREAK_EVENT:

                case CtrlTypes.CTRL_CLOSE_EVENT:

                case CtrlTypes.CTRL_LOGOFF_EVENT:

                case CtrlTypes.CTRL_SHUTDOWN_EVENT:

                    isclosing = true;

                    task.Stop();

                    break;
                default:
                    break;

            }
            return true;

        }

        #region unmanaged

        // Declare the SetConsoleCtrlHandler function

        // as external and receiving a delegate.



        [DllImport("Kernel32")]
        public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);



        // A delegate type to be used as the handler routine

        // for SetConsoleCtrlHandler.

        public delegate bool HandlerRoutine(CtrlTypes CtrlType);



        // An enumerated type for the control messages

        // sent to the handler routine.

        public enum CtrlTypes
        {

            CTRL_C_EVENT = 0,

            CTRL_BREAK_EVENT,

            CTRL_CLOSE_EVENT,

            CTRL_LOGOFF_EVENT = 5,

            CTRL_SHUTDOWN_EVENT

        }



        #endregion

    }
}

